package com.lkd.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.RegionDTO;
import com.lkd.entity.Region;
import com.lkd.mapper.OrderMapper;
import com.lkd.mapper.RegionMapper;
import com.lkd.report.Collect;
import com.lkd.result.TableInfo;
import com.lkd.service.RegionService;
import com.lkd.vo.RegionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/17 13:28
 * @Description:
 **/
@Service
@Slf4j
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {
    private final RegionMapper regionMapper;
    private final OrderMapper orderMapper;
    public RegionServiceImpl(RegionMapper regionMapper, OrderMapper orderMapper) {
        this.regionMapper = regionMapper;
        this.orderMapper = orderMapper;
    }
    /**
    * @Description 列表查询
    * @Param [regionDTO]
    * @Date 2025/8/17 13:46
    **/
    public TableInfo<Region> list(RegionDTO regionDTO) {
        PageHelper.startPage(regionDTO.getPageIndex(),regionDTO.getPageSize());
        List<Region> regionPlusList = regionMapper.selectRegionList(regionDTO);
        Page<Region> page = (Page<Region>) regionPlusList;
        return new TableInfo<>(regionDTO.getPageIndex(), regionDTO.getPageSize(),page.getPages(),page.getTotal(),page.getResult());
    }

    /**
    * @Description 更新
    * @Param [regionDTO]
    * @Date 2025/8/17 13:45
    **/
    public boolean update(RegionDTO regionDTO) {
        return regionMapper.update(regionDTO);
    }

    /**
    * @Description 新增
    * @Param [regionDTO]
    * @Date 2025/8/17 13:45
    **/
    public boolean save(RegionDTO regionDTO) {
        return regionMapper.insert(regionDTO);
    }

    /**
    * @Description 删除
    * @Param [regionId]
    * @Date 2025/8/17 13:45
    **/
    public boolean delete(String regionId) {
        return regionMapper.deleteById(regionId);
    }

    @Override
    public RegionVO getRegion(String regionId) {
        RegionVO regionVO = new RegionVO();
        Region region = regionMapper.getRegionById(regionId);
        BeanUtils.copyProperties(region,regionVO);
        return regionVO;
    }

    @Override
    public Collect regionCollect(String beginTime, String endTime) {
        List<LocalDate> dateList = formatTime(beginTime, endTime);
        Collect regionCollect = new Collect();
        List<String> xAxis = new ArrayList<>();
        List<Integer> series = new ArrayList<>();
        for(LocalDate date : dateList){
            LocalDateTime begin = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of(date, LocalTime.MAX);
            series.add(orderMapper.regionCountPrice(begin,end));
            xAxis.add(orderMapper.regionCount(begin,end));
            regionCollect.setXAxis(xAxis);
            regionCollect.setSeries(series);
        }
        return regionCollect;
    }

    public List<LocalDate> formatTime(String start, String end) {
        LocalDate startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd").toLocalDate();
        LocalDate endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd").toLocalDate();;
        List<LocalDate> dataList = new ArrayList<>();
        dataList.add(startTime);
        //循环，日期+1，添加到dateList
        while (!startTime.isEqual(endTime)) {
            startTime = startTime.plusDays(1);
            dataList.add(startTime);
        }
        return dataList;
    }
}
